<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 120px;"
        class="filter-item"
        placeholder="请输入版本号"
        v-model="queryForm.version"
      >
      </el-input>
      <el-select
        clearable
        style="width: 150px"
        class="filter-item"
        v-model="queryForm.clientOs"
        placeholder="请选择设备系统"
      >
        <el-option
          v-for="(val, key) in Dicts.CLIENT_TYPE"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <!-- <el-select clearable style="width: 150px" class="filter-item" v-model="queryForm.type" placeholder="请选择应用类型">
        <el-option v-for="(val, key) in Dicts.APP_APPLICATION_TYPE" :key="key" :label="val" :value="key">
        </el-option>
      </el-select> -->
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
      <el-button
        v-permission="'appSoftware:add'"
        class="filter-item"
        @click="openAddDialog"
        type="success"
        icon="el-icon-plus"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="records" style="width: 100%">
      <el-table-column prop="version" label="版本号" width="150" align="center">
      </el-table-column>
      <el-table-column
        prop="sequence"
        label="内部打包序号"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="clientOs"
        label="设备系统"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.CLIENT_TYPE[scope.row.clientOs] }}
        </template>
      </el-table-column>
      <el-table-column prop="type" label="应用类型" width="150" align="center">
        <template slot-scope="scope">
          {{ Dicts.APP_TYPE[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="forced"
        label="强制升级"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.IS_TRUE[scope.row.forced] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="versionUrl"
        label="版本更新地址"
        width="300"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="changeLog"
        label="版本摘要说明"
        width="300"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="remark"
        label="版本备注信息"
        width="300"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="modifyTime"
        label="修改时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'appSoftware:update'"
            size="mini"
            type="primary"
            @click="editAppVersion(scope.row)"
            title="修改"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            v-permission="'appSoftware:delete'"
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            title="删除"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="'新增App软件版本'"
      :visible.sync="dialogAddFormVisible"
      width="50%"
    >
      <el-form
        :model="addForm"
        label-width="160px"
        :rules="addFormRules"
        ref="addForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="版本号" prop="version">
              <el-input
                v-model="addForm.version"
                placeholder="请输入版本号"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="内部打包序号" prop="sequence">
              <el-input
                v-model="addForm.sequence"
                placeholder="请输入内部打包序号"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="设备系统" prop="clientOs">
              <el-select
                v-model="addForm.clientOs"
                placeholder="请选择设备系统"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.CLIENT_TYPE"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应用类型" prop="type">
              <el-select
                v-model="addForm.type"
                placeholder="请选择应用类型"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.APP_TYPE"
                  :key="key"
                  :label="val"
                  :value="key"
                  v-if="key != 2"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="强制升级" prop="forced">
              <el-select
                v-model="addForm.forced"
                placeholder="请选择是否强制升级"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.IS_TRUE"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本更新地址" prop="versionUrl">
              <el-input
                v-model="addForm.versionUrl"
                placeholder="请输入版本更新地址"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="版本摘要说明" prop="changeLog">
              <el-input
                type="textarea"
                v-model="addForm.changeLog"
                placeholder="请输入版本摘要说明"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本备注信息" prop="remark">
              <el-input
                type="textarea"
                v-model="addForm.remark"
                placeholder="请输入版本备注信息"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="execAddForm">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="'修改App软件版本'"
      :visible.sync="dialogEditFormVisible"
      width="50%"
    >
      <el-form
        :model="editForm"
        label-width="160px"
        :rules="editFormRules"
        ref="editForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="版本号" prop="version">
              <el-input
                :readonly="true"
                v-model="editForm.version"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="内部打包序号" prop="sequence">
              <el-input
                v-model="editForm.sequence"
                placeholder="请输入内部打包序号"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="设备系统" prop="clientOs">
              <el-select
                v-model="editForm.clientOs"
                placeholder="请选择设备系统"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.CLIENT_TYPE"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="应用类型" prop="type">
              <el-select
                v-model="editForm.type"
                placeholder="请选择应用类型"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.APP_TYPE"
                  :key="key"
                  :label="val"
                  :value="key"
                  v-if="key != 2"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="强制升级" prop="forced">
              <el-select
                v-model="editForm.forced"
                placeholder="请选择是否强制升级"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.IS_TRUE"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本更新地址" prop="versionUrl">
              <el-input
                v-model="editForm.versionUrl"
                placeholder="请输入版本更新地址"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="版本摘要说明" prop="changeLog">
              <el-input
                type="textarea"
                v-model="editForm.changeLog"
                placeholder="请输入版本摘要说明"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本备注信息" prop="remark">
              <el-input
                type="textarea"
                v-model="editForm.remark"
                placeholder="请输入版本备注信息"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="execEditForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryAppVersion,
  addAppVersion,
  editAppVersion,
  deleteAppVersion
} from '@/api/terminal'

export default {
  name: 'app',
  data() {
    var checkNum = (rule, value, callback) => {
      if (!/^[0-9]*$/g.test(value)) {
        callback(new Error('只支持数字'))
      } else {
        callback()
      }
    }
    var checkVersion = (rule, value, callback) => {
      if (/^[0-9a-zA-Z.]*$/g.test(value)) {
        callback()
      } else {
        callback(new Error('格式不正确'))
      }
    }
    return {
      dialogAddFormVisible: false,
      dialogEditFormVisible: false,
      loading: false,
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        status: undefined,
        title: undefined
      },
      addFormRules: {
        version: [
          { required: true, message: '请输入版本号', trigger: 'blur' },
          { validator: checkVersion, trigger: 'blur' }
        ],
        sequence: [
          { required: true, message: '请输入内部打包序号', trigger: 'blur' },
          { validator: checkNum, trigger: 'blur' }
        ],
        clientOs: [
          { required: true, message: '请选择设备系统', trigger: 'change' }
        ],
        type: [
          { required: true, message: '请选择应用类型', trigger: 'change' }
        ],
        forced: [
          { required: true, message: '请选择是否强制升级', trigger: 'change' }
        ],
        versionUrl: [
          { required: true, message: '请输入版本更新地址', trigger: 'blur' }
        ]
      },
      addForm: {},
      editFormRules: {
        sequence: [
          { required: true, message: '请输入内部打包序号', trigger: 'blur' },
          { validator: checkNum, trigger: 'blur' }
        ],
        clientOs: [
          { required: true, message: '请选择设备系统', trigger: 'change' }
        ],
        type: [
          { required: true, message: '请选择应用类型', trigger: 'change' }
        ],
        forced: [
          { required: true, message: '请选择是否强制升级', trigger: 'change' }
        ],
        versionUrl: [
          { required: true, message: '请输入版本更新地址', trigger: 'blur' }
        ]
      },
      editForm: {},
      typeOptions: Dicts.DOORCARD_STATUS,
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryAppVersion(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleDelete(rowId) {
      this.$confirm('此操作将删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteAppVersion(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    },
    openAddDialog() {
      this.addForm = {}
      this.dialogAddFormVisible = true
      this.$refs.addForm && this.$refs.addForm.resetFields()
    },
    execAddForm() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          addAppVersion(this.addForm).then(({ data: { code, msg, data } }) => {
            if (code === 0) {
              this.doQuery()
              this.dialogAddFormVisible = false
            }
            this.$message({
              showClose: true,
              message: msg,
              type: code === 0 ? 'success' : 'error'
            })
          })
        } else {
          return false
        }
      })
    },
    openEditDialog() {
      this.editForm = {}
      this.dialogEditFormVisible = true
      this.$refs.editForm && this.$refs.editForm.resetFields()
    },
    editAppVersion(row) {
      this.openEditDialog()
      this.editForm = {
        rowId: row.rowId,
        version: row.version,
        sequence: row.sequence,
        clientOs: row.clientOs,
        type: row.type,
        forced: row.forced,
        encryptValue: row.encryptValue,
        versionUrl: row.versionUrl,
        changeLog: row.changeLog,
        remark: row.remark
      }
    },
    execEditForm() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          editAppVersion(this.editForm).then(
            ({ data: { code, msg, data } }) => {
              if (code === 0) {
                this.doQuery()
                this.dialogEditFormVisible = false
              }
              this.$message({
                showClose: true,
                message: msg,
                type: code === 0 ? 'success' : 'error'
              })
            }
          )
        } else {
          return false
        }
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
